package demo5;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-08-03
 * Time: 22:09
 */
public class TestTree {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val){
            this.val = val;
        }
    }
    public TreeNode root;//根节点
    //二叉树的非递归遍历--前序
    public void preOrderNor1(TreeNode root){
        if(root==null) return ;
        //使用栈
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode cur = stack.pop();
            System.out.println(cur.val);
            if(cur.right!=null){
                stack.push(cur.right);
            }
            if(cur.left!=null){
                stack.push(cur.left);
            }
        }
    }
    public TreeNode preOrderNor2(TreeNode root){
        if(root==null) return null;
        //使用队列
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.println(cur.val);
            if(cur.left!=null){
                queue.offer(cur.left);
            }
            if(cur.right!=null){
                queue.offer(cur.right);
            }
        }
        return null;
    }

    //二叉树的非递归遍历--中序
    public TreeNode inOrderNor(TreeNode root){
        List<Integer> ret = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur!=null||!stack.isEmpty()){
            while(cur!=null){
                stack.push(cur.left);
                cur = cur.left;
            }
            //没有左,记录自己,然后在看右边
            TreeNode top = stack.pop();
            ret.add(top.val);
            if(top.right!=null){
                cur = top.right;
            }
        }
        return null;
    }

    //二叉树的非递归遍历--后序
    public TreeNode postOrderNor(TreeNode root){
        //使用两个栈--后序遍历
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        stack1.push(root);
        while(!stack1.isEmpty()){
            TreeNode cur = stack1.pop();
            stack2.push(cur);
            if(cur.left!=null){
                stack1.push(cur.left);
            }
            if(cur.right!=null){
                stack1.push(cur.right);
            }
        }
        while(!stack2.isEmpty()){
            System.out.println(stack2.pop());
        }
        return null;
    }

    //二叉树的非递归遍历--后序
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if(root==null) return ret;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while(cur!=null||(!stack.isEmpty())){
            while(cur!=null){//一直遍历到左子树
                stack.push(cur);
                cur = cur.left;
            }
            //此时cur已经为空
            TreeNode top = stack.peek();//知道栈顶元素左孩子为空
            //但不知道右孩子是什么情况,因为是先左在右
            if(top.right==null||top.right==prev){
                //证明无右孩子,并且要记录此节点为栈顶的前一个节点
                //避免多次遍历
                stack.pop();
                ret.add(top.val);
                prev = top;
            }else {
                //如果有右孩子指向右孩子
                cur = top.right;
            }
        }
        return ret;
    }


    public static void main(String[] args) {

    }


}
